package com.linkage.jz.bankruptcy.activity.Project.ZQUser;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.RegistLogin.PhoneLoginActivity;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.bean.ZQUserInfo;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.utils.SignUtil;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;

import org.json.JSONObject;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * A login screen that offers login via email/password.
 */
public  class EditZQUserBaseProxyerInfoActivity extends BaseActivity implements OnClickListener{
    private static final String TAG = EditZQUserBaseProxyerInfoActivity.class.getSimpleName();

    private ShapeLoadingDialog shapeLoadingDialog ;
    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
        top_mune1.setOnClickListener(this);
//        top_mune2.setOnClickListener(this);
    }

    @Override
    public void initTitle() {
        top_title = findViewById(R.id.top_title);
            top_title.setText("代理人资料");
//        top_mune.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initView() {

    }

    ImageView mLeftLogo;
    ImageView mRightLogo;
    EditText et_Name,et_org,et_account,et_email,et_addr,et_otherphone;
    Button btn_login;
    TextView top_title,getPwd;
    Button top_back;
    TextView et_missonName,et_missonNote,tv_missonexer,tv_missontime,top_mune1;
    CheckBox ck_time,ck_timed;
    LinearLayout ll_timed;
    RelativeLayout rl_lookper,rl_joinper;
    Switch switch_show;




    int date_type = 1;
    int date_type_flag = 0;
//    private static String mProID;
//    public static void start(Context context,MyProInfo info) {
//        Intent intent = new Intent(context, CreateProFlowActivity.class);
//        mInfo = info;
//        context.startActivity(intent);
//    }

    private static ZQUserInfo mInfo;

    public static void start(Context context, ZQUserInfo info) {
        Intent intent = new Intent(context, EditZQUserBaseProxyerInfoActivity.class);
        mInfo = info;
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_edit_zqbase_proxyer);

        initview();

        String user = SPUtils.getInstance(Consts.SPNAME).getString("loginUser");
        String pwd = SPUtils.getInstance(Consts.SPNAME).getString("loginPwd");

//        if(!user.equalsIgnoreCase(""))
//        {
//            et_username.setText(user);
//            et_password.setText(pwd);
//        }
        shapeLoadingDialog = new ShapeLoadingDialog(this);
        super.onCreate(savedInstanceState);
    }

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层



        top_back = findViewById(R.id.top_back);
        top_mune1 = findViewById(R.id.top_mune1);

        et_Name = findViewById(R.id.et_Name);
        et_org = findViewById(R.id.et_org);
        et_account = findViewById(R.id.et_account);
        et_email = findViewById(R.id.et_email);
        et_addr = findViewById(R.id.et_addr);
        et_otherphone = findViewById(R.id.et_otherphone);



//        rl_joinper.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                date_type = 1;
//
//                Intent intent = new Intent(CreateProMissonActivity.this, TalkPersonSelActivity.class);
//                startActivity(intent);
//
//            }
//        });



    }

    @Override
    public void onClick(View view) {

            String phone = "";
            String code = "";
            String pwd = "";
            switch (view.getId()) {
                case R.id.btn_login:

                    break;
                case R.id.ll_phonelogin:
                    PhoneLoginActivity.start(this);
                    finish();
                    break;
                case R.id.top_back:
                    finish();
                    break;
                case R.id.top_mune1:
//                    saveData();

                    break;
                case R.id.wxlogin:

//                    QQLogin();
                    break;
                default:
                    break;
            }


    }

    public void saveData()
    {



        String str_et_Name  = et_Name.getText().toString();
        if ("".equalsIgnoreCase(str_et_Name)) {
            Toast.makeText(this, "名称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        String str_et_org  = et_org.getText().toString();
        if ("".equalsIgnoreCase(str_et_org)) {
            Toast.makeText(this, "所属机构不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        shapeLoadingDialog.setLoadingText("提交信息中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();
        if(BankruptcyApp.tokeninfo == null|| BankruptcyApp.tokeninfo.getToken() == null)
        {
            getToken();
            return;
        }

        OkHttpClient okHttpClient = new OkHttpClient();
        HashMap<String, String> params = new HashMap<String, String>();
        Random random = new Random();
        String rom = random.nextLong()+"";
        String timestamp =System.currentTimeMillis()+"";


        String sign = SignUtil.createSign(Consts.APPKEY,params);
        String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken","");

        RequestBody requestBody = new FormBody.Builder()
                .add("appid",Consts.APPID)
                .add("nonce",rom)
                .add("timestamp", timestamp)
                .add("token", BankruptcyApp.tokeninfo.getToken())
                .add("userToken", userToken)

                .add("managersTeamId", mInfo.getId())
                .add("sysUserId", mInfo.getSysUserId())
                .add("username", et_Name.getText().toString())
                .add("subsidiaryOrgan", et_org.getText().toString())
                .add("email", et_email.getText().toString())
                .add("otherContactNumber", et_otherphone.getText().toString())


                .add("province", "")
                .add("city", "")
                .add("region", "")
                .add("address", et_addr.getText().toString())
                .build();

        String url = Consts.WEBURL_EDITMEMBER;

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                Log.d(TAG, "onFailure: " + e.getMessage());
                shapeLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.d(TAG, response.protocol() + " " +response.code() + " " + response.message());
                Headers headers = response.headers();


                shapeLoadingDialog.dismiss();
                String str = response.body().string();
                try{
                    JSONObject object = new JSONObject(str);
                    if(object.optInt("code") == 0)
                    {

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                Toast.makeText(EditZQUserBaseProxyerInfoActivity.this,
                                        "提交成功",Toast.LENGTH_SHORT).show();
                                finish();

                            }

                        });
                    }else{
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                Toast.makeText(EditZQUserBaseProxyerInfoActivity.this,
                                        "提交失败",Toast.LENGTH_SHORT).show();


                            }

                        });
                    }

                    str = "";
                }catch (Exception ex)
                {

                    ex.printStackTrace();
                }
            }
        });


    }


    public void setDate(){

        //通过自定义控件AlertDialog实现
        AlertDialog.Builder builder = new AlertDialog.Builder(EditZQUserBaseProxyerInfoActivity.this);
        View view = (LinearLayout) getLayoutInflater().inflate(R.layout.layout_dateshow, null);
        final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
        //设置日期简略显示 否则详细显示 包括:星期\周
        datePicker.setCalendarViewShown(false);
        //初始化当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), null);
        //设置date布局
        builder.setView(view);
        builder.setTitle("设置日期信息");
        builder.setPositiveButton("确  定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //日期格式
                StringBuffer sb = new StringBuffer();
                sb.append(String.format("%d-%02d-%02d ",
                        datePicker.getYear(),
                        datePicker.getMonth() + 1,
                        datePicker.getDayOfMonth()));
//                        oplandate.setText(sb);
//                        //赋值后面闹钟使用
                int mYear = datePicker.getYear();
                int mMonth = datePicker.getMonth();
                int mDay = datePicker.getDayOfMonth();



                    tv_missontime.setText(sb.toString() + " 00:00");

                dialog.cancel();
            }
        });
        builder.setNegativeButton("取  消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.create().show();
    }





}

